// pages/accounting_subject/accounting_subject.js
let listBehavior = require('../../behavior/list')

Page({
  behaviors: [ listBehavior, ],
  /**
   * 页面的初始数据
   */
  data: {
    // 筛选条件
    keyList: [ { field_id: 'account_id', field_name: '科目名称', }, ],
    searchVal: '',
    accountOptList: [],

    tabActive: -1,
    tabList: [
      { id: '', name: '全部', },
    ],
  },

  // 混合查询
  searchComb(e) {
    let val = e.detail.iptVal
    this.setData({
      searchVal: val,
    })
    this.getFirstPage()
  },
  onTabChange(e) {
    let { index, } = e.detail
    this.setData({
      tabActive: index,
    })
    this.getFirstPage()
  },

  // 接口
  ready() {
    this.getTabList()
    this.getAcctOptList()
  },
  getTabList() {
    wx.$get(3, 'Account/select_account_type').then(res => {
      if(res.errcode !== 0) return
      let rows = res.data.row
      rows = [
        { id: '', name: '全部', },
        ...rows,
      ]
      this.setData({
        tabActive: 0,
        tabList: rows,
      })
    })
  },
  // 获取科目名称选项列表
  getAcctOptList() {
    wx.$get(3, 'Account/select_account').then(res => {
      if(res.errcode !== 0) return
      let rows = res.data.row
      this.setData({
        accountOptList: rows,
      })
    })
  },
  getDataList() {
    if(!this.data.auth.tableSee) {
      let { total, page, limit, } = this.data
      wx.$toast('抱歉，您没有权限查看该模块')
      this.setData({
        total,
        page,
        limit,
      })
      return
    }

    let { page, limit, list, searchVal, accountOptList, tabActive, tabList, } = this.data,
        post = {
          page,
          limit,
          oby: 'account_list'
        }
    let SearchWhere = []
    if(searchVal) {
      // SearchWhere.push({
      //   CompareValue: searchVal,
      //   CompareWith: 'account_id',
      //   Operator: 'like',
      //   or: 1,
      // })
      let arr = accountOptList.filter(item => item.name.indexOf(searchVal) > -1)
      arr.forEach(item => {
        SearchWhere.push({
          CompareValue: item.id,
          CompareWith: 'account_id',
          Operator: '=',
          or: 1,
        })
      })
    }
    if(tabActive > 0) {
      SearchWhere.push({
        CompareValue: tabList[tabActive].id,
        CompareWith: 'account_type_id',
        Operator: '=',
        or: 0,
      })
    }
    if(SearchWhere.length) post.SearchWhere = SearchWhere
    wx.$post(3, 'Account/query_account', post).then(res => {
      if(res.errcode !== 0) return
      let rows = res.data.row
      if(page !== 1) rows = [ ...list, ...rows, ]
      this.setData({
        list: rows,
        total: res.data.total,
        page,
      })
    })
  },

  /**
   * 生命周期函数--监听页面加载
   */
  async onLoad(options) {
    wx.$setBarTitle();
    this.ready()
    let { entity, } = options,
        auth = await wx.$getAuth(entity)
    this.data.auth = auth

    this.getDataList()
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  },
})
